package com.pearl.generator.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.generator.common.PageResult;
import com.pearl.generator.common.Result;
import com.pearl.generator.mapper.GeneratorPlanConfigMapper;
import com.pearl.generator.pojo.entity.DataSourceEntity;
import com.pearl.generator.pojo.entity.GeneratorPlanConfigEntity;
import com.pearl.generator.pojo.request.DataSourceQuery;
import com.pearl.generator.pojo.request.GeneratorPlanRequest;
import com.pearl.generator.pojo.request.PlanConfigQuery;
import com.pearl.generator.pojo.vo.DataSourceVO;
import com.pearl.generator.pojo.vo.GeneratorPlanVO;
import com.pearl.generator.service.ControllerConfigService;
import com.pearl.generator.service.IGeneratorPlanConfigService;
import com.pearl.generator.util.GeneratorConfigUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 方案配置表 前端控制器
 * </p>
 *
 * @author TangDan
 * @since 2022-12-16 16:15:20
 */
@Api(value = "GeneratorController", tags = "方案配置")
@RestController
@RequestMapping("/generatorPlanConfig")
public class GeneratorPlanConfigController {

    @Resource
    IGeneratorPlanConfigService generatorPlanConfigService;

    @Resource
    GeneratorPlanConfigMapper generatorPlanConfigMapper;

    @ApiOperation(value = "查询所有方案", produces = "application/json")
    @GetMapping("/list")
    public Result<List<GeneratorPlanVO>> list() {
        List<GeneratorPlanVO> generatorPlanVOS = generatorPlanConfigMapper.listPlanVO();
        return Result.success(generatorPlanVOS);
    }

    @ApiOperation(value = "新增或配置方案", produces = "application/json")
    @PostMapping("/saveOrUpdate")
    public Result<?> list(@RequestBody GeneratorPlanRequest request) {
        GeneratorPlanConfigEntity entity = BeanUtil.copyProperties(request, GeneratorPlanConfigEntity.class);
        entity.setStrategyId(1L);
        generatorPlanConfigService.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "分页查询", produces = "application/json")
    @GetMapping("/page")
    public PageResult<List<GeneratorPlanVO>> page(PlanConfigQuery query) {
        Page<GeneratorPlanVO> page = generatorPlanConfigMapper.pagePlanVO(new Page<>(query.getPageNo(), query.getPageSize()),query.getName());
        List<GeneratorPlanVO> result = page.getRecords();
        return PageResult.success(result, page.getTotal());
    }

    @ApiOperation(value = "根据ID删除", produces = "application/json")
    @GetMapping("/remove")
    public Result<?> removeById(@RequestParam Long id) {
        GeneratorConfigUtils.checkDefaultConfigDelete(id);
        generatorPlanConfigService.removeById(id);
        return Result.success();
    }
}
